﻿using DL.Animation;
using DL.Characters.Player.FSM.StateMachine;
using UnityEngine;

namespace DL.Characters.Player.FSM.State
{
    public class PlayerOnGroundState : PlayerMovementState
    {
        public PlayerOnGroundState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(
            playerStateMachine, property)
        {
        }

        public override void Update()
        {
            //检查是否掉落进入Fall状态
            // StateMachine.ChangeStateByAniName<PlayerAirFallingState>("Falling");
            if (StateMachine.AnimatorStateInfo.IsName("Falling") &&
                !StateMachine.IsSameStateWithCurrentState<PlayerAirFallingState>() &&
                !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.ChangeState<PlayerAirFallingState>();
            }
        }

        public override void FixedUpdate()
        {
            base.FixedUpdate();
            MoveController.CharacterMoveInterface(Self.forward,
                ReusableData.InputDirection.sqrMagnitude * ReusableData.InputMult);
            EnterMoveStateInterruptOtherAction();
        }
        
        //正常移动时那些自定义位移需要被打断
        protected void EnterMoveStateInterruptOtherAction()
        {
            if (ReusableData.InputDirection != Vector2.zero &&
                MoveController.CharacterIsOnGround &&
                !StateMachine.AnimatorStateInfo.IsTag(AnimatorTag.Motion) && !StateMachine.AnimatorIsInTransition)
            {
                StateMachine.Player.CustomMovementController.InterruptCustomMovement();
                Debug.Log("播放动画MotionState");
                Animator.CrossFade("MotionState", ReusableData.AniTransitionTime);
            }
        }
    }
}